package com.sqstudio.control
{
	import com.sqstudio.common.Sys;
	import com.sqstudio.common.json.JSON;
	import com.sqstudio.event.CalendarEvent;
	import com.sqstudio.model.Model;
	import com.sqstudio.view.WeatherPanel;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.system.System;
	import flash.utils.Timer;
	
	/**
	 *天气控制器 
	 * @author Nestor
	 * 
	 */
	public class WeatherController
	{
		private var weatherPanel:WeatherPanel;
		public function WeatherController()
		{
			this.weatherPanel = new WeatherPanel();
			Sys.STAGE.addChild(this.weatherPanel);
			this.weatherPanel.x = 25;
			this.weatherPanel.y = 50;
			
			CalendarEvent.Dispatcher.addEventListener(CalendarEvent.UPDATE_TIME,parseTime);
			getWeatherData();
		}
		
		/**
		 *初始化时间 
		 * 
		 */		
		private function parseTime(e:Event):void{
			var timeObj:Object = Model.getInstance().time_obj;
			var strArr:Array = String(timeObj.time.time).split(":");
			
			this.timeDate = new Date(strArr[0],strArr[1],strArr[2],strArr[3],strArr[4],strArr[5]);
			this.timeDate.setSeconds(this.timeDate.seconds+1);
			var timer:Timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER,timeHandler);
			timer.start();
		}
		private var timeDate:Date;
		/**
		 *计时器响应 
		 * @param e
		 * 
		 */		
		private function timeHandler(e:TimerEvent):void
		{
			var date:Date = this.timeDate;
			var hour:String = date.getHours()<10?"0"+date.getHours():String(date.getHours());
			var min:String = date.getMinutes()<10?"0"+date.getMinutes():String(date.getMinutes());
			var sec:String = date.getSeconds()<10?"0"+date.getSeconds():String(date.getSeconds());
			var time:String = hour+" : "+min+" : "+sec;
			this.weatherPanel.updateTime(time);
			this.timeDate.setSeconds(this.timeDate.seconds+1);
		}
		
		
		
		/***************************************************
		 * 以下提供天气数据
		 * 
		 */	
		
		private function getWeatherData():void{
			var link:String = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
			
			var urlLaoder:URLLoader = new URLLoader();
			urlLaoder.addEventListener(Event.COMPLETE,comIpHandler);
			urlLaoder.addEventListener(IOErrorEvent.IO_ERROR,error);
			urlLaoder.load(new URLRequest(link));
		}
		
		
		private var province:String,city:String;
		private var weatherArr:Array=[];
		protected function comIpHandler(e:Event):void
		{
			trace(e.currentTarget.data);
			var obj:Object = JSON.decode(e.currentTarget.data);
			
			province = obj.province;
			city = obj.city;
			if(city=="")city = obj.desc;
			remove(e,comIpHandler)
			var id1:String = "http://m.weather.com.cn/data5/city.xml";
			remote(id1,id1ComHandler);
			
		}
		
		private function id1ComHandler(e:Event):void{
			var cid:String = "-1";
			var str:String = e.currentTarget.data;
			trace(e.currentTarget.data);
			if(str.indexOf("<html") != -1){
				weatherBj();
				return;
			}
			var arr:Array = str.split(",");
			for(var i:int=0;i<arr.length;i++){
				var str1:String = String(arr[i]);
				if(str1.indexOf(province) != -1){
					var arr1:Array = str1.split("|");
					cid = arr1[0];
					break;
				}
			}
			remove(e,id1ComHandler);
			var id2:String =  "http://m.weather.com.cn/data5/city"+cid+".xml";
			remote(id2,id2ComHandler);
		}
		
		private function id2ComHandler(e:Event):void
		{
			trace(e.currentTarget.data);
			var cid:String = "-1";
			var str:String = e.currentTarget.data;
			if(str.indexOf("<html") != -1){
				weatherBj();
				return;
			}
			var arr:Array = str.split(",");
			for(var i:int=0;i<arr.length;i++){
				var str1:String = String(arr[i]);
				if(str1.indexOf(city) != -1){
					var arr1:Array = str1.split("|");
					cid = arr1[0];
					break;
				}
			}
			remove(e,id2ComHandler)
			var id3:String =  "http://m.weather.com.cn/data5/city"+cid+".xml";
			remote(id3,id3ComHandler);
			
		}		
		
		private function id3ComHandler(e:Event):void
		{
			trace(e.currentTarget.data);
			var cid:String = "-1";
			var str:String = e.currentTarget.data;
			if(str.indexOf("<html") != -1){
				weatherBj();
				return;
			}
			var arr:Array = str.split(",");
			for(var i:int=0;i<arr.length;i++){
				var str1:String = String(arr[i]);
				if(str1.indexOf(city) != -1){
					var arr1:Array = str1.split("|");
					cid = arr1[0];
					break;
				}
			}
			remove(e,id3ComHandler)
			var id4:String =  "http://m.weather.com.cn/data5/city"+cid+".xml";
			remote(id4,id4ComHandler);
		}		
		
		private function id4ComHandler(e:Event):void
		{
			trace(e.currentTarget.data);
			var str:String = e.currentTarget.data;
			if(str.indexOf("<html") != -1){
				weatherBj();
				return;
			}
			
			var cid:String = str.split("|")[1];
			remove(e,id4ComHandler)
			var id5:String =  "http://m.weather.com.cn/data/"+cid+".html";
			remote(id5,id5ComHandler);
		}		
		
		private function id5ComHandler(e:Event):void
		{
			trace(e.currentTarget.data);
			var str:String = e.currentTarget.data;
			
			if(str.indexOf("<html") != -1){
				weatherBj();
				return;
			}
			
			var obj:Object = JSON.decode(str);
			
			var w1:String = obj.weatherinfo.weather1+" "+obj.weatherinfo.temp1+" "+obj.weatherinfo.wind1;
			var w2:String = obj.weatherinfo.weather2+" "+obj.weatherinfo.temp2+" "+obj.weatherinfo.wind2;
			var w3:String = obj.weatherinfo.weather3+" "+obj.weatherinfo.temp3+" "+obj.weatherinfo.wind3;
			
			weatherArr = [w1,w2,w3];
			this.weatherPanel.updateWeather(city,weatherArr);
		}		
		private function remote(url:String,comFun:Function):void{
			var vars:URLVariables=new URLVariables();
			vars.link=url;
			var urlrqs:URLRequest = new URLRequest("http://wannianrili.duapp.com/getHttpContent.php");
			urlrqs.method = URLRequestMethod.POST;
			urlrqs.data= vars;
			var urlLaoder:URLLoader = new URLLoader();
			urlLaoder.addEventListener(Event.COMPLETE,comFun);
			urlLaoder.addEventListener(IOErrorEvent.IO_ERROR,error);
			urlLaoder.load(urlrqs);
		}
		private function remove(event:Event,comFun:Function):void{
			var urlLoader:URLLoader = event.currentTarget as URLLoader;
			urlLoader.removeEventListener(Event.COMPLETE,comFun);
			urlLoader.removeEventListener(IOErrorEvent.IO_ERROR,error);
		}
		private function error(event:IOErrorEvent):void{
			trace(event.text);
			weatherBj();
		}
		
		
		private function weatherBj():void{	
			
			var link:String = "http://m.weather.com.cn/data/101010100.html";
			remote(link,comHandler);
		}
		
		private function comHandler(event:Event):void
		{
			var str:String = event.currentTarget.data;
			var obj:Object = JSON.decode(str);
			
			var w1:String = obj.weatherinfo.weather1+" "+obj.weatherinfo.temp1+" "+obj.weatherinfo.wind1;
			var w2:String = obj.weatherinfo.weather2+" "+obj.weatherinfo.temp2+" "+obj.weatherinfo.wind2;
			var w3:String = obj.weatherinfo.weather3+" "+obj.weatherinfo.temp3+" "+obj.weatherinfo.wind3;
			
			weatherArr = [w1,w2,w3];
			city = "北京";
			this.weatherPanel.updateWeather(city,weatherArr);
		}
	}
}